using System;
using EPiServer.Common;
using EPiServer.Common.Security;
using EPiServer.Community.NewsFeed;

namespace EPiAbstractions.Community.NewsFeed
{
    public class NewsFeedHandlerFacade : INewsFeedHandlerFacade
    {
        private static NewsFeedHandlerFacade _instance;

        public NewsFeedHandlerFacade()
        {
            NewsFeedHandler.StoryToActorFeedAdded += OnStoryToActorFeedAdded;
            NewsFeedHandler.StoryToNewsFeedPublished += OnStoryToNewsFeedPublished;
            NewsFeedHandler.StoryToActorFeedRemoved += OnStoryToActorFeedRemoved;
            NewsFeedHandler.ActionAdded += OnActionAdded;
            NewsFeedHandler.ActionUpdated += OnActionUpdated;
            NewsFeedHandler.ActionRemoved += OnActionRemoved;
        }

        public static NewsFeedHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new NewsFeedHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region INewsFeedHandlerFacade Members

        public event EPiServerCommonEventHandler StoryToActorFeedAdded;
        public event EPiServerCommonEventHandler StoryToNewsFeedPublished;
        public event EPiServerCommonEventHandler StoryToActorFeedRemoved;
        public event EPiServerCommonEventHandler ActionAdded;
        public event EPiServerCommonEventHandler ActionUpdated;
        public event EPiServerCommonEventHandler ActionRemoved;

        public virtual NewsFeedStoryCollection GetActorFeed(IUser actor, NewsFeedActionCollection actions, DateTime startDate,
                                                            DateTime endDate, Int32 page, Int32 pageSize)
        {
            return NewsFeedHandler.GetActorFeed(actor, actions, startDate, endDate, page, pageSize);
        }

        public virtual NewsFeedStoryCollection GetActorFeed(IUser actor, NewsFeedActionCollection actions, DateTime startDate,
                                                            DateTime endDate, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return NewsFeedHandler.GetActorFeed(actor, actions, startDate, endDate, page, pageSize, out totalItems);
        }

        public virtual NewsFeedStoryCollection GetNewsFeed(IUser actor, NewsFeedActionCollection actions, DateTime startDate,
                                                           DateTime endDate, Int32 page, Int32 pageSize)
        {
            return NewsFeedHandler.GetNewsFeed(actor, actions, startDate, endDate, page, pageSize);
        }

        public virtual NewsFeedStoryCollection GetNewsFeed(IUser actor, NewsFeedActionCollection actions, DateTime startDate,
                                                           DateTime endDate, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return NewsFeedHandler.GetNewsFeed(actor, actions, startDate, endDate, page, pageSize, out totalItems);
        }

        public virtual NewsFeedStoryCollection GetTargetFeed(IEntity target, NewsFeedActionCollection actions, DateTime startDate,
                                                             DateTime endDate, Int32 page, Int32 PageSize)
        {
            return NewsFeedHandler.GetTargetFeed(target, actions, startDate, endDate, page, PageSize);
        }

        public virtual NewsFeedStoryCollection GetTargetFeed(IEntity target, NewsFeedActionCollection actions, DateTime startDate,
                                                             DateTime endDate, Int32 page, Int32 PageSize, out Int32 totalItems)
        {
            return NewsFeedHandler.GetTargetFeed(target, actions, startDate, endDate, page, PageSize, out totalItems);
        }

        public virtual NewsFeedStory AddStoryToActorFeed(NewsFeedStory story)
        {
            return NewsFeedHandler.AddStoryToActorFeed(story);
        }

        public virtual NewsFeedStoryActor GetStoryActor(Int32 id)
        {
            return NewsFeedHandler.GetStoryActor(id);
        }

        public virtual NewsFeedAction AddAction(NewsFeedAction action)
        {
            return NewsFeedHandler.AddAction(action);
        }

        public virtual void UpdateAction(NewsFeedAction action)
        {
            NewsFeedHandler.UpdateAction(action);
        }

        public virtual void RemoveAction(NewsFeedAction action)
        {
            NewsFeedHandler.RemoveAction(action);
        }

        public virtual NewsFeedAction GetAction(Int32 id)
        {
            return NewsFeedHandler.GetAction(id);
        }

        public virtual NewsFeedAction GetAction(String name)
        {
            return NewsFeedHandler.GetAction(name);
        }

        public virtual NewsFeedActionCollection GetActions()
        {
            return NewsFeedHandler.GetActions();
        }

        public virtual void RemoveStory(NewsFeedStory story)
        {
            NewsFeedHandler.RemoveStory(story);
        }

        public virtual NewsFeedStory GetStory(Int32 id)
        {
            return NewsFeedHandler.GetStory(id);
        }

        #endregion

        public virtual void OnStoryToActorFeedAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (StoryToActorFeedAdded != null)
                StoryToActorFeedAdded(sender, e);
        }

        public virtual void OnStoryToNewsFeedPublished(String sender, EPiServerCommonEventArgs e)
        {
            if (StoryToNewsFeedPublished != null)
                StoryToNewsFeedPublished(sender, e);
        }

        public virtual void OnStoryToActorFeedRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (StoryToActorFeedRemoved != null)
                StoryToActorFeedRemoved(sender, e);
        }

        public virtual void OnActionAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ActionAdded != null)
                ActionAdded(sender, e);
        }

        public virtual void OnActionUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ActionUpdated != null)
                ActionUpdated(sender, e);
        }

        public virtual void OnActionRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ActionRemoved != null)
                ActionRemoved(sender, e);
        }
    }
}